/// dotnet new console
/// dotnet add package BenchmarkDotNet 
/// paste this code in Program.cs
/// dotnet publish -r win10-x64 -c Release
/// run an .exe file in publish forder

using System;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

[MemoryDiagnoser]
public class Program
{
    const int Count = 100000; 

    static void Main(string[] args)
    {
        BenchmarkRunner.Run<Program>(); 
    }

    [Benchmark]
    public void StringFormat()
    {
        var results = new string[Count]; 
        for (int i = 0; i < Count; i++)
        {
            results[i] = "This is a test #" + i + " with a moment " + DateTime.Now + " so i will " + i + "th time try to concat something";  

        }

    }

    [Benchmark]
    public void PlusOperator()
    {
        var results = new string[Count]; 
        for (int i = 0; i < Count; i++)
        {
            results[i] = String.Format("This is a test #{0} with a moment {1} so i will {0}th time try to concat something", i, DateTime.Now);  

        }
    }

    [Benchmark]
    public void DollarSign()
    {
        var results = new string[Count]; 
        for (int i = 0; i < Count; i++)
        {
            results[i] = $"This is a test #{i} with a moment {DateTime.Now} so i will {i}th time try to concat something";  

        }
    }

    [Benchmark]
    public void StringBuilder()
    {
        var results = new string[Count]; 
        for (int i = 0; i < Count; i++)
        {
            var sb = new StringBuilder(90);
            sb.Append("This is a test #");
            sb.Append(i);
            sb.Append(" with a moment ");
            sb.Append(DateTime.Now.ToString()); 
            sb.Append(" so i will ");
            sb.Append(i);
            sb.Append("th time try to concat something");

            results[i] = sb.ToString();
        }
    }

    [Benchmark]
    public void ClearableStringBuilder()
    {
        var results = new string[Count];
        var sb = new StringBuilder(90); 
        for (int i = 0; i < Count; i++)
        {
            sb.Append("This is a test #");
            sb.Append(i);
            sb.Append(" with a moment ");
            sb.Append(DateTime.Now.ToString()); 
            sb.Append(" so i will ");
            sb.Append(i);
            sb.Append("th time try to concat something");

            results[i] = sb.ToString();
            sb.Clear(); 
        }
    }
}

